﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M006_GOODS_GROUP : FormBase
    {
        #region 变量定义

        XGWMS.BLL.BLL_M006_GOODS_GROUP bll_M006_GOODS_GROUP = new XiaoGang.Soft.XGWMS.BLL.BLL_M006_GOODS_GROUP();
        private const string dataName = "商品分类";
        bool isInit = true;

        #endregion

        #region 方法

        public M006_GOODS_GROUP()
        {
            InitializeComponent();
        }

        public M006_GOODS_GROUP(bool isNew)
        {
            InitializeComponent();
        }

        /// <summary>
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>	
        protected void ClearControl()
        {
            txtGOODS_GROUP_ID.Text = "";
            txtGOODS_GROUP_NM.Text = "";
            txtMEMO.Text = "";
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>	
        protected void SetTabIndex()
        {
            int intIndex = 0;
            txtGOODS_GROUP_ID.TabIndex = intIndex; intIndex++;
            txtGOODS_GROUP_NM.TabIndex = intIndex; intIndex++;
            txtMEMO.TabIndex = intIndex; intIndex++;
            btnInsert.TabIndex = intIndex; intIndex++;            
            btnExit.TabIndex = intIndex; intIndex++;            
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>        
        protected void SetEnable()
        {
            grpGoodsGroupInfo.Enabled = true;
            txtGOODS_GROUP_ID.Enabled = true;
            txtGOODS_GROUP_NM.Enabled = true;
            txtMEMO.Enabled = true;
            btnInsert.Enabled = true;
            grpSearchResult.Enabled = true;
            grdmentGoodsGroup.Enabled = true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>
        protected void InitProcess()
        {
            ClearControl();
            SetTabIndex();
            SetEnable();
            InitialGridView();
            isInit = false;
        }

        /// <summary>	
        /// <p><b>方法名称</b>InitialGridView</p>	
        /// <p><b>概要説明	:</b>Grid初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>
        protected void InitialGridView ( )
        {
            grdmentGoodsGroup.AllowUserToAddRows = false;
            grdmentGoodsGroup.AllowUserToResizeColumns = true;
            grdmentGoodsGroup.AllowUserToDeleteRows = false;
            grdmentGoodsGroup.AllowUserToResizeRows = false;
            grdmentGoodsGroup.ColumnHeadersHeight = 30;
            grdmentGoodsGroup.ColumnHeadersVisible = true;
            grdmentGoodsGroup.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdmentGoodsGroup.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentGoodsGroup.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdmentGoodsGroup.ReadOnly = true;
            grdmentGoodsGroup.RowHeadersVisible = false;
            grdmentGoodsGroup.RowTemplate.Resizable = DataGridViewTriState.False;
            grdmentGoodsGroup.RowTemplate.Height = 25;
            grdmentGoodsGroup.RowTemplate.ReadOnly = true;
            grdmentGoodsGroup.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdmentGoodsGroup.ScrollBars = ScrollBars.Both;
            grdmentGoodsGroup.TabStop = false;

            grdmentGoodsGroup.Columns.Add ("NO.", "NO.");
            grdmentGoodsGroup.Columns.Add ("GOODS_GROUP_ID", "商品分类编号");
            grdmentGoodsGroup.Columns.Add ("GOODS_GROUP_NM", "商品分类名称");
            grdmentGoodsGroup.Columns.Add("MEMO", "备注");
            grdmentGoodsGroup.Columns.Add ("OrderNum", "排序");
            DataGridViewButtonColumn btnUp = new DataGridViewButtonColumn() { Text = "↑", Width = 50, HeaderText = "上移", SortMode = DataGridViewColumnSortMode.NotSortable,Resizable= DataGridViewTriState.False};
            grdmentGoodsGroup.Columns.Add(btnUp);
            DataGridViewButtonColumn btnDown = new DataGridViewButtonColumn() { Text = "↓", Width = 50, HeaderText = "下移", SortMode = DataGridViewColumnSortMode.NotSortable, Resizable = DataGridViewTriState.False };
            grdmentGoodsGroup.Columns.Add(btnDown);
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn() { Text = "删除", Width = 80, HeaderText = "删除", SortMode = DataGridViewColumnSortMode.NotSortable, Resizable = DataGridViewTriState.False };
            grdmentGoodsGroup.Columns.Add(btnDelete);
            grdmentGoodsGroup.Columns.Add("TRANS_STATE", "");

            grdmentGoodsGroup.Columns["NO."].Width = 40;
            grdmentGoodsGroup.Columns["NO."].Frozen = true;
            grdmentGoodsGroup.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentGoodsGroup.Columns["NO."].Resizable = DataGridViewTriState.False;
            grdmentGoodsGroup.Columns["NO."].SortMode = DataGridViewColumnSortMode.NotSortable;

            grdmentGoodsGroup.Columns["GOODS_GROUP_ID"].Width = 140;
            grdmentGoodsGroup.Columns["GOODS_GROUP_ID"].Resizable = DataGridViewTriState.False;
            grdmentGoodsGroup.Columns["GOODS_GROUP_ID"].SortMode = DataGridViewColumnSortMode.NotSortable;

            grdmentGoodsGroup.Columns["GOODS_GROUP_NM"].Width = 160;
            grdmentGoodsGroup.Columns["GOODS_GROUP_NM"].Resizable = DataGridViewTriState.True;
            grdmentGoodsGroup.Columns["GOODS_GROUP_NM"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentGoodsGroup.Columns["GOODS_GROUP_NM"].Frozen = true;

            grdmentGoodsGroup.Columns["OrderNum"].Width = 50;
            grdmentGoodsGroup.Columns["OrderNum"].Resizable = DataGridViewTriState.False;
            grdmentGoodsGroup.Columns["OrderNum"].SortMode = DataGridViewColumnSortMode.NotSortable;
            
            grdmentGoodsGroup.Columns["MEMO"].Width = 180;
            grdmentGoodsGroup.Columns["MEMO"].Resizable = DataGridViewTriState.True;
            grdmentGoodsGroup.Columns["MEMO"].SortMode = DataGridViewColumnSortMode.NotSortable;

            grdmentGoodsGroup.Columns["TRANS_STATE"].Visible = false;
        }        

        /// <summary>	
        /// <p><b>方法名称</b>ClearGrid</p>	
        /// <p><b>概要説明	:</b>Gridをオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>
        protected void BindGridData()
        {
            if (isInit)
            {
                return;
            }
            XGWMS.MODEL.MOD_M006_GOODS_GROUP model = new XiaoGang.Soft.XGWMS.MODEL.MOD_M006_GOODS_GROUP() 
            { GOODS_GROUP_ID = txtGOODS_GROUP_ID.Text.Trim()
                , GOODS_GROUP_NM = txtGOODS_GROUP_NM.Text.Trim()
                , UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim()
                , TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim()
                , MEMO = txtMEMO.Text.Trim()};
            bool isShowDelete = false;
            if (ckbShowDelete.Checked)
            {
                isShowDelete = true;
            }
            grdmentGoodsGroup.Rows.Clear();
            ReturnValue rvGoodsGroup=bll_M006_GOODS_GROUP.SearchProcess(model,isShowDelete);
            if (rvGoodsGroup.IsSuccess == false)
            {
                Method.MessageShow(rvGoodsGroup.ErrorID, dataName);
                return;
            }
            if (0 == rvGoodsGroup.ResultDataSet.Tables[0].Rows.Count)
            {
                grpSearchResult.Text = "没有符合条件到数据";
                return;
            }
            for (int i = 0; i < rvGoodsGroup.ResultDataSet.Tables[0].Rows.Count; i++)
            {               
                grdmentGoodsGroup.Rows.Insert(grdmentGoodsGroup.Rows.Count,
                    (i + 1).ToString(),
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["GOODS_GROUP_ID"].ToString().Trim(),
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["GOODS_GROUP_NM"].ToString().Trim(),
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["MEMO"].ToString().Trim(),
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["OrderNum"].ToString().Trim(),
                    "↑",
                    "↓",
                    "删除",
                    rvGoodsGroup.ResultDataSet.Tables[0].Rows[i]["TRANS_STATE"].ToString().Trim());
            }
            grpSearchResult.Text = String.Format("检索结果  共{0}条", rvGoodsGroup.ResultDataSet.Tables[0].Rows.Count);
            SetGridBackColor();
        }
    
        /// <summary>
        /// 设置已删除的数据的背景色
        /// </summary>
        /// <param name="GridType"></param>
        private void SetGridBackColor()
        {
            for (int i = 0; i < grdmentGoodsGroup.Rows.Count; i++)
            {
                if (grdmentGoodsGroup.Rows[i].Cells["TRANS_STATE"].Value.ToString().Trim() == "3")
                {
                    grdmentGoodsGroup.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
            }
        }

        /// <summary>
        /// 检测该分类有没有没使用
        /// </summary>
        /// <param name="strGoodsGroupID"></param>
        /// <returns>true:使用或发生错误 false:未使用</returns>
        protected bool IsGoodsGroupUsed(string strGoodsGroupID)
        {
            ReturnValue rvReturnValue = bll_M006_GOODS_GROUP.GoodsGroupDeleteCheck(strGoodsGroupID);
            if (rvReturnValue.IsSuccess)
            {
                if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    Method.MessageShow("W11D01", dataName);//本@正在使用!不能删除!
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else                
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return true;
            }
        }
        #endregion

        #region 事件

        private void GOODS_GROUP_006_Load(object sender, EventArgs e)
        {
            //初始化
            InitProcess();
            //查询数据
            BindGridData();
            //设置当前焦点为ID输入框
            txtGOODS_GROUP_ID.Focus();
        }
        
        /// <summary>
        /// 删除数据或进行排序调整
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdmentGoodsGroup_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            int intColumnIndex = e.ColumnIndex;
            //删除
            if (intColumnIndex == 7 && e.RowIndex != -1)
            {
                //物理彻底删除
                if (grdmentGoodsGroup.CurrentRow.Cells["TRANS_STATE"].Value.ToString() == "3")
                {
                    this.Cursor = Cursors.WaitCursor;
                    //检查该分类是否正在被使用
                    if (IsGoodsGroupUsed(grdmentGoodsGroup.CurrentRow.Cells["GOODS_GROUP_ID"].Value.ToString()))
                    {
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    if (Method.MessageShow("Q22017",dataName) == DialogResult.Yes)//本条数据已被逻辑删除。确定要进行彻底删除吗?
                    {   
                        ReturnValue result = bll_M006_GOODS_GROUP.PhysicalDeleteProcess(grdmentGoodsGroup.CurrentRow.Cells["GOODS_GROUP_ID"].Value.ToString());
                        if (result.IsSuccess)
                        {
                            BindGridData();
                        }
                        else
                        {
                            Method.MessageShow(result.ErrorID,dataName);
                        }
                    }
                    this.Cursor = Cursors.Default;
                    return;
                }
                //逻辑删除
                if (Method.MessageShow("Q22007",dataName) == DialogResult.Yes)//确定要删除该条数据吗?
                {
                    this.Cursor = Cursors.WaitCursor;
                    try
                    {
                        XGWMS.MODEL.MOD_M006_GOODS_GROUP model =
                            new XiaoGang.Soft.XGWMS.MODEL.MOD_M006_GOODS_GROUP()
                            {
                                GOODS_GROUP_ID = grdmentGoodsGroup.CurrentRow.Cells["GOODS_GROUP_ID"].Value.ToString(),
                                UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim(),
                                TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim()
                            };
                        ReturnValue result = bll_M006_GOODS_GROUP.DeleteProcess(model);
                        if (result.IsSuccess)
                        {
                            BindGridData();
                        }
                        else
                        {
                            Method.MessageShow(result.ErrorID, dataName);
                        }
                    }
                    catch
                    {
                        Method.MessageShow("E11004",dataName);//An unknown error process!
                    }
                    finally
                    {
                        this.Cursor = Cursors.Default;
                    }
                }
            }
            //向上
            if (intColumnIndex == 5 && e.RowIndex != -1 && e.RowIndex != 0)
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    //更新排序编号
                    //MessageBox.Show("向上了一下");
                    ReturnValue result = bll_M006_GOODS_GROUP.UpdateOrder(grdmentGoodsGroup.CurrentRow.Cells["GOODS_GROUP_ID"].Value.ToString()
                        , GlobalVal.gloUserInfo.UPDATER_ID
                        , GlobalVal.gloUserInfo.TERMINAL_CD
                        , -1);
                    if (result.IsSuccess)
                    {
                        BindGridData();
                    }
                    else
                    {
                        Method.MessageShow(result.ErrorID, dataName);
                    }
                }
                catch
                {
                    Method.MessageShow("E11004", dataName);//An unknown error process!
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
            //向下
            if (intColumnIndex == 6 && e.RowIndex != -1 && e.RowIndex != grdmentGoodsGroup.Rows.Count - 1)
            {   
                this.Cursor = Cursors.WaitCursor;

                try
                {
                    //更新排序编号
                    //MessageBox.Show("向下了一下");
                    ReturnValue result = bll_M006_GOODS_GROUP.UpdateOrder(grdmentGoodsGroup.CurrentRow.Cells["GOODS_GROUP_ID"].Value.ToString()
                        , GlobalVal.gloUserInfo.UPDATER_ID
                        , GlobalVal.gloUserInfo.TERMINAL_CD
                        , 1);
                    if (result.IsSuccess)
                    {
                        BindGridData();
                    }
                    else
                    {
                        Method.MessageShow(result.ErrorID, dataName);
                    }
                }
                catch
                {
                    Method.MessageShow("E11004", dataName);//An unknown error process!
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            string strButtonName = ((Button)sender).Name;
            switch (strButtonName)
            {
                #region btnInsert

                case "btnInsert":
                    {
                        using (M006GoodsGroupEdit form = new M006GoodsGroupEdit())
                        {
                            if (form.ShowDialog() == DialogResult.OK)
                            {
                                //刷新Grid数据
                                BindGridData();
                            }
                        }
                        break;
                    }

                #endregion

                #region btnExit

                case "btnExit":
                    {
                        this.Close();
                    }
                    break;

                #endregion
            }
        }

        private void AllTextBox_Focus(object sender, EventArgs e)
        {            
            try
            {
                switch (((TextBox)sender).Name)
                {
                    case "txtGOODS_GROUP_ID":
                        ((TextBox)sender).SelectAll();
                        ((TextBox)sender).ImeMode = ImeMode.Disable;
                        break;
                    case "txtGOODS_GROUP_NM":
                        ((TextBox)sender).SelectAll();
                        ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                        break;
                }
            }
            catch
            {                
                txtMEMO.ImeMode = ImeMode.Hiragana;
            }
        }

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M006_GOODS_GROUP_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    AllButton_Click(btnInsert, e);
                    break;
                case Keys.F8:
                case Keys.Escape:
                    AllButton_Click(btnExit, e);
                    break; 
            }
        }

        /// <summary>
        /// 查询条件改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryChanged(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            BindGridData();
        }

        /// <summary>
        /// 双击进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdmentGoodsGroup_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (0 == grdmentGoodsGroup.Rows.Count 
                || e.RowIndex == -1
                || e.ColumnIndex == 5
                || e.ColumnIndex == 6
                || e.ColumnIndex == 7)
            {
                return;
            }
            string strGroupID = grdmentGoodsGroup.CurrentRow.Cells["GOODS_GROUP_ID"].Value.ToString();
            string strGroupName = grdmentGoodsGroup.CurrentRow.Cells["GOODS_GROUP_NM"].Value.ToString();
            string strGroupMemo = grdmentGoodsGroup.CurrentRow.Cells["MEMO"].Value.ToString();
            string strOrderNum = grdmentGoodsGroup.CurrentRow.Cells["OrderNum"].Value.ToString();
            if (new M006GoodsGroupEdit(strGroupID, strGroupName, strGroupMemo, strOrderNum).ShowDialog() == DialogResult.OK)
            {
                //刷新Grid数据
                BindGridData();
            }
        }

        #endregion
    }
}